package cn.iocoder.yudao.module.asasc.dal.mysql.profitreport;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.asasc.controller.admin.profitreport.vo.ProfitReportPageReqVO;
import cn.iocoder.yudao.module.asasc.dal.dataobject.profitreport.ProfitReportDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 投入产出报告 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface ProfitReportMapper extends BaseMapperX<ProfitReportDO> {

    default PageResult<ProfitReportDO> selectPage(ProfitReportPageReqVO reqVO) {
        List<String> countriesList = reqVO.getCountries() != null ?
                Stream.of(reqVO.getCountries()).collect(Collectors.toList()) : null;
        List<Long> adamIds = reqVO.getAdamIds() != null ?
                Stream.of(reqVO.getAdamIds()).collect(Collectors.toList()) : null;

        QueryWrapper<ProfitReportDO> queryWrapper = new QueryWrapper<ProfitReportDO>()
                .eq(reqVO.getOrgId() != null, "org_id", reqVO.getOrgId())
                .in(countriesList != null, "country", countriesList)
                .in(adamIds != null, "adam_id", adamIds)
                .between("rep_date", reqVO.getRepDate()[0], reqVO.getRepDate()[1]);

        // 动态设置 select 字段
        List<String> selectFields = new ArrayList<>();
        List<String> orderByFields = new ArrayList<>();

        // 根据 dimension 字段进行分组
        if (reqVO.getDimension() != null) {
            for (String dim : reqVO.getDimension()) {
                switch (dim) {
                    case "repDate":
                        queryWrapper.groupBy("rep_date");
                        selectFields.add("rep_date");
                        orderByFields.add("rep_date");
                        break;
                    case "country":
                        queryWrapper.groupBy("country");
                        selectFields.add("country");
                        orderByFields.add("country");
                        break;
                    case "adamId":
                        queryWrapper.groupBy("adam_id");
                        selectFields.add("adam_id");
                        orderByFields.add("adam_id");
                        break;
                    default:
                        throw new IllegalArgumentException("Unknown dimension: " + dim);
                }
            }
        }

        selectFields.add("SUM(ad_spend) AS adSpend");
        selectFields.add("SUM(asc_sales) AS ascSales");
        selectFields.add("SUM(asc_proceeds) AS ascProceeds");
        selectFields.add("SUM(ad_taps) AS adTaps");
        selectFields.add("SUM(ad_installs) AS adInstalls");
        selectFields.add("AVG(ad_ttr) AS adTtr");
        selectFields.add("AVG(ad_conversion_rate) AS adConversionRate");
        selectFields.add("SUM(asc_impressions) AS ascImpressions");
        selectFields.add("SUM(asc_taps) AS ascTaps");
        selectFields.add("SUM(asc_installs) AS ascInstalls");
        selectFields.add("SUM(asc_re_installs) AS ascReInstalls");
        selectFields.add("SUM(asc_upt_installs) AS ascUptInstalls");
        selectFields.add("AVG(asc_ttr) AS ascTtr");
        selectFields.add("AVG(asc_conversion_rate) AS ascConversionRate");
        selectFields.add("SUM(naturally_installs) AS naturallyInstalls");
        selectFields.add("AVG(ad_cpa) AS adCpa");
        selectFields.add("AVG(ad_cpt) AS adCpt");
        selectFields.add("AVG(ad_cpm) AS adCpm");
        selectFields.add("SUM(sub_trial) AS subTrial");
        selectFields.add("SUM(sub_renewal) AS subRenewal");
        selectFields.add("SUM(sub_refund) AS subRefund");
        selectFields.add("SUM(sub_refund_amount) AS subRefundAmount");
        selectFields.add("SUM(sub_refund_proceeds) AS subRefundProceeds");

        // 使用 join 将 selectFields 转换为一个字符串，并传递给 select
        queryWrapper.select(String.join(", ", selectFields));
        //排序
        queryWrapper.orderBy(true, false, String.join(", ", orderByFields));

        return selectPage(reqVO, queryWrapper);
    }
}